//
//  XYJGZViewController.m
//  徙游记-01
//
//  Created by hcios on 2017/7/14.
//  Copyright © 2017年 鹿林. All rights reserved.
//

#import "XYJGZViewController.h"
#import "XYJFSModel.h"
#import "XYJFSTableViewCell.h"
#import "UserViewController.h"

@interface XYJGZViewController ()<UITableViewDelegate,UITableViewDataSource>{
    UITableView *_tableView;
    
    NSInteger page;
    
    NSMutableArray *_dataArr;
}

@end

@implementation XYJGZViewController

-(instancetype)init{
    if (self = [super init]) {
        _dataArr = [[NSMutableArray alloc] init];
        page = 1;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initTable];
    [_tableView.mj_header beginRefreshing];
}


-(void)refreshGetData:(BOOL)isRefresh{
    
    isRefresh ? page = 1 : page++;
    
    NSString *urlString = @"http://chanyouji.com/api/friends/users.json?user_id=708809";
    
    NSDictionary *param = @{@"page":@(page)};
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:urlString parameters:param progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (isRefresh) {
            [_dataArr removeAllObjects];
            _tableView.mj_footer.state = MJRefreshStateIdle;
        }
        
        if ([responseObject count] > 0) {
            for (NSDictionary *dict in responseObject) {
                XYJFSModel *model = [[XYJFSModel alloc] initWithDict:dict];
                [_dataArr addObject:model];
            }
        }
        if (_dataArr.count < 10) {
            [_tableView.mj_footer endRefreshingWithNoMoreData];
        }
        [_tableView reloadData];
        
        if (!isRefresh && [responseObject count] == 0) {
            [_tableView.mj_footer endRefreshingWithNoMoreData];
        }else [self endRefresh];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"网络请求失败");
    }];
}



-(void)endRefresh{
    if(page == 1){
        [_tableView.mj_header endRefreshing];
    }else [_tableView.mj_footer endRefreshing];
}


-(void)initTable{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 274) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.showsVerticalScrollIndicator = NO;
    //刷新
    __weak typeof(self) weakSelf = self;
    
    _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf refreshGetData:YES];
    }];
    
    _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [weakSelf refreshGetData:NO];
    }];
    [self.view addSubview:_tableView];
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString * cellID = [NSString  stringWithFormat:@"cellID%ld%ld",(long)[indexPath section],(long)[indexPath row]];
    XYJFSTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[XYJFSTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    cell.model = _dataArr[indexPath.row];
    
    return cell;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}


- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}


@end
